package com.qicheng.wlhyspider.anhui.controller;

import com.qicheng.wlhyspider.anhui.service.AhCompanyService;
import com.qicheng.wlhyspider.anhui.service.AhFinancialService;
import com.qicheng.wlhyspider.common.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 安徽资金单
 */
@RestController
@RequestMapping("/anhui/financial")
@Slf4j
public class AhFinancialController {
    private final AhCompanyService ahCompanyService;
    private final AhFinancialService ahFinancialService;
    private final RedisUtils redisUtils;

    public AhFinancialController(AhCompanyService ahCompanyService, AhFinancialService ahFinancialService, RedisUtils redisUtils) {
        this.ahCompanyService = ahCompanyService;
        this.ahFinancialService = ahFinancialService;
        this.redisUtils = redisUtils;
    }


    @GetMapping("/save")
    public Result save(@RequestParam("page") Integer page,
                       @RequestParam("startDate") String startDate,
                       @RequestParam("endDate") String endDate,
                       @RequestHeader("token") String token) {
        if (StringUtils.isBlank(token)) {
            return Results.error("header里面的token不能为空");
        }
        redisUtils.set(Constants.AH_TOKEN, token);
        String companyId = ahCompanyService.save();
        return ahFinancialService.saveAll(page, startDate, endDate, companyId);
    }

    @GetMapping("/saveDetail")
    public Result saveDetail(@RequestHeader("token") String token) {
        if (StringUtils.isBlank(token)) {
            return Results.error("header里面的token不能为空");
        }
        redisUtils.set(Constants.AH_TOKEN, token);
        String companyId = ahCompanyService.save();
        return ahFinancialService.saveDetail(companyId);
    }
}
